import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
const Login = () => import('views/login/Login.vue')
const Home = () => import('views/home/Home.vue')
const Welcome = () => import('views/home/children/Welcome.vue')
const User = () => import('views/home/children/users/User.vue')
const Role = () => import('views/home/children/power/Role.vue')
const Right = () => import('views/home/children/power/Right.vue')
const Category = () => import('views/home/children/goods/Category.vue')
const ClassParameters = () => import('views/home/children/goods/ClassParameters.vue')
const Goods = () => import('views/home/children/goods/Goods.vue')
const AddGood = () => import('views/home/children/goods/AddGood.vue')
const Order = () => import('views/home/children/order/Order.vue')
const Report = () => import('views/home/children/report/Report.vue')
const routes = [
  {
    path: '/',
    redirect: '/login'
  },

  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        name: 'Welcome',
        component: Welcome,
        // 给未来的路由做权限限制
        meta: {
          auth: true // 如果为true，证明该用户访问该组件的时候需要先登录
        }
      },
      {
        path: '/users',
        name: 'User',
        component: User,
        // 给未来的路由做权限限制
        meta: {
          auth: true // 如果为true，证明该用户访问该组件的时候需要先登录
        }
      },
      {
        path: '/roles',
        name: 'Role',
        component: Role,
        // 给未来的路由做权限限制
        meta: {
          auth: true // 如果为true，证明该用户访问该组件的时候需要先登录
        }
      },
      {
        path: '/rights',
        name: 'Right',
        component: Right,
        // 给未来的路由做权限限制
        meta: {
          auth: true // 如果为true，证明该用户访问该组件的时候需要先登录
        }
      },
      {
        path: '/categories',
        name: 'Category',
        component: Category,
        // 给未来的路由做权限限制
        meta: {
          auth: true // 如果为true，证明该用户访问该组件的时候需要先登录
        }
      },
      {
        path: '/params',
        name: 'ClassParameters',
        component: ClassParameters,
        // 给未来的路由做权限限制
        meta: {
          auth: true // 如果为true，证明该用户访问该组件的时候需要先登录
        }
      },
      {
        path: '/goods',
        name: 'Goods',
        component: Goods,
        // 给未来的路由做权限限制
        meta: {
          auth: true // 如果为true，证明该用户访问该组件的时候需要先登录
        }
      },
      {
        path: '/addgood',
        name: 'AddGood',
        component: AddGood,
        // 给未来的路由做权限限制
        meta: {
          auth: true // 如果为true，证明该用户访问该组件的时候需要先登录
        }
      },
      {
        path: '/orders',
        name: 'Order',
        component: Order,
        meta: {
          auth: true
        }
      },
      {
        path: '/reports',
        name: 'Report',
        component: Report,
        meta: {
          auth: true
        }
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 全局导航守卫,判断是否需要登录
router.beforeEach((to, from, next) => {
  console.log(to.meta.auth)
  console.log(from)
  console.log(to.matched)// []
  if (window.sessionStorage.getItem('token')) {
    next()
  } else {
    if (to.meta.auth) {
      next({
        path: '/login'
      })
    } else {
      next()// 放行
    }
  }
})

export default router
